package com.mvvm.start.main.mvvm.v.fragment.main

import android.app.Activity.RESULT_OK
import androidx.activity.result.contract.ActivityResultContracts
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.mvvm.start.base.base.BaseFragment
import com.mvvm.start.base.utils.CommonUtils
import com.mvvm.start.common.mvvm.vm.PublicViewModle
import com.mvvm.start.main.databinding.FragmentHomeBinding
import com.mvvm.start.main.mvvm.v.adapter.BottomAdapter
import com.mvvm.start.main.mvvm.v.fragment.home.CloudTaskFragment
import com.mvvm.start.main.mvvm.v.fragment.home.LocalTaskFragment


/**
 * @author lmy
 * @功能: 首页Fragment
 * @Creat 12/23/20 12:09 PM
 * @Compony 465008238@qq.com
 */
class HomeFragment : BaseFragment<PublicViewModle, FragmentHomeBinding>() {

    //StartActivityForResult新的Api方式
    val addTaskActivityLaunch =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
            // 处理返回的 Uri
            if (result.resultCode == RESULT_OK) {
            }
        }

    override fun initViews() {
        viewBinding.toolbar.setPadding(0, CommonUtils.getStatusBarHight(requireContext()), 0, 0)
        viewBinding.homeModle = mViewModel
        initTablayoutViewPager()
    }

    /**
     * 初始化TableLayout
     */
    private fun initTablayoutViewPager() {
        var tabTitle = mutableListOf("fragment1", "fragment2")
        viewBinding.viewpager.apply {
            adapter = BottomAdapter(
                childFragmentManager, lifecycle, mutableListOf(
                    LocalTaskFragment(),
                    CloudTaskFragment()
                )
            )
            offscreenPageLimit = 1
            isUserInputEnabled = false//禁止滑动因为 有rv侧滑删除容易造成误触所以禁止home的左右滑动
        }
//        tab_layout.setupWithViewPager(viewpager)
        TabLayoutMediator(viewBinding.tabLayout, viewBinding.viewpager) { tab, position ->
            tab.text = tabTitle[position];
        }.attach()
        viewBinding.tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                if (tab?.text?.equals("云端脚本") == true) {
                } else {
                }
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
            }
        });
        viewBinding.viewpager.registerOnPageChangeCallback(object :
            ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {

            }
        })
    }

    override fun initObserve() {
    }

    override fun initData() {
    }
}